package com.welse.modulebiz.controller;

import com.welse.modulebiz.domain.*;
import com.welse.modulebiz.domain.dto.Register;
import com.welse.modulebiz.domain.entity.Member;
import com.welse.modulebiz.service.MailService;
import com.welse.modulebiz.service.MemberService;
import com.welse.modulebiz.utils.CodeUtil;
import com.welse.modulebiz.utils.DateUtil;
import com.welse.modulebiz.utils.IpAddressUtil;
import com.welse.modulebiz.utils.Md5Util;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@RequestMapping("/user/register")
@RestController
public class UserRegisterController {

    @Autowired
    private MemberService memberService;
    @Autowired
    private MailService mailService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    // 注册
    @PostMapping("/email")
    public Result register(Register register) {
        Member member = new Member();
        member.setUserid(register.getUserid());
        member.setEmail(register.getEmail());
        member.setUname(register.getNickname());
        member.setPwd(register.getPassword());
        String ip = IpAddressUtil.getIpAddress(request);
        //检查用户名或者邮箱是否存在
        Member member1 = memberService.findByUserid(member.getUserid());
        Member member2 = memberService.findByEmail(member.getEmail());

        if (member1 != null) {
            return Result.error("用户已存在");
        }
        if (member2 != null) {
            return Result.error("邮箱已存在");
        }
        else {
            // 从Redis中获取验证码，使用统一的键格式
            String code = stringRedisTemplate.opsForValue().get("email:code:" + register.getEmail());
            // 验证码是否正确（添加null值检查）
            if(code != null && register.getEmailCode() != null && register.getEmailCode().equals(code)) {
                member.setJointime(DateUtil.getCurrentDate());
                member.setUptime(DateUtil.getMonthEndTime());
                member.setJoinip(ip);
                member.setLoginip(ip);
                member.setPwd(Md5Util.getMD5String(member.getPwd()));
                memberService.register(member);
                // 验证成功后删除验证码，防止重复使用
                stringRedisTemplate.delete("email:code:" + register.getEmail());
                return Result.success();
            } else {
                // 更详细的错误信息
                if (code == null) {
                    return Result.error("验证码已过期或不存在，请重新获取");
                } else if (register.getEmailCode() == null) {
                    return Result.error("请输入验证码");
                } else {
                    return Result.error("邮箱验证码错误");
                }
            }
        }

    }

}
